package com.task.web.controller.core;

import javax.annotation.Resource;

import com.task.core.vo.resp.PluginDetailRespVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.task.common.annotation.Log;
import com.task.common.core.controller.BaseController;
import com.task.common.core.domain.AjaxResult;
import com.task.common.core.page.TableDataInfo;
import com.task.common.enums.BusinessType;
import com.task.common.utils.SecurityUtils;
import com.task.core.dto.request.PluginRequestDTO;
import com.task.core.service.CheckService;
import com.task.core.service.PluginService;

import io.swagger.annotations.Api;

/**
 * @author 迪迦.
 * @date 2024/11/4 14:32
 */
@Api("")
@RestController
@RequestMapping("/plugin")
public class PluginController extends BaseController {


  @Resource
  private PluginService pluginService;

  @Resource
  private CheckService checkService;


  @PreAuthorize("@ss.hasPermi('plugin:list')")
  @GetMapping("/getPluginByPage")
  public TableDataInfo getPluginByPage(PluginRequestDTO request) {
    startPage(request.getPageNum(), request.getPageSize());
    return getDataTable(pluginService.getPluginByPage(request));
  }


  @PreAuthorize("@ss.hasPermi('plugin:option')")
  @GetMapping("/getPluginForOption")
  public TableDataInfo getPluginForOption(PluginRequestDTO request) {
    return getDataTable(pluginService.getPluginForOption(request));
  }

  @Log(title = "Plugin管理", businessType = BusinessType.INSERT)
  @PostMapping("/addPlugin")
  public AjaxResult addPlugin(@RequestBody PluginRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    pluginService.addPlugin(request);
    return success();
  }

  @Log(title = "Plugin管理", businessType = BusinessType.UPDATE)
  @PutMapping("/modifyPlugin")
  public AjaxResult modifyPlugin(@RequestBody PluginRequestDTO request) {
    checkService.check("data_plugin", request.getCode());
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    pluginService.modifyPlugin(request);
    return success();
  }

  @Log(title = "Plugin管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/deletePlugin/{pluginCode}")
  public AjaxResult deletePlugin(@PathVariable String pluginCode) {
    checkService.check("data_plugin", pluginCode);
    pluginService.deletePlugin(pluginCode, SecurityUtils.getLoginUser().getUserId());
    return success();
  }
  
  @GetMapping ("/getPluginDetail/{pluginCode}")
  @PreAuthorize("@ss.hasPermi('system:plugin:edit')")
  public AjaxResult getPluginDetail(@PathVariable String pluginCode) {
    checkService.check("data_plugin", pluginCode);
    PluginDetailRespVo pluginDetailRespVo  = pluginService.getPluginDetail(pluginCode);
    return success(pluginDetailRespVo);
  }
}
